package com.yz.administrator.jiukuaijiu;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.yz.administrator.jiukuaijiu.zhekou.fragment.FiveFragment;
import com.yz.administrator.jiukuaijiu.zhekou.fragment.FourthFragment;
import com.yz.administrator.jiukuaijiu.zhekou.fragment.OneFragment;
import com.yz.administrator.jiukuaijiu.zhekou.fragment.ThirdFragment;
import com.yz.administrator.jiukuaijiu.zhekou.fragment.TwoFragment;
import com.yz.administrator.jiukuaijiu.zhekou.help.FragmentHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.main_navigation)
    LinearLayout mainNavigation;


    private long mNowtime;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private FragmentManager mManager = getSupportFragmentManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragment();
        initView();
    }

    public  void initView()
    {
          for(int i=0;i<mainNavigation.getChildCount();i++)
            {
                ImageView view = (ImageView) mainNavigation.getChildAt(i);
                if(i==0)
                {
                    view.setSelected(true);
                }
                view.setTag(i);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position= (int) v.getTag();
                        selectPosition(position);
                        FragmentHelper.switchFragment(mManager,mFragmentList,position,R.id.main_container,0,0);

                    }
                });


            }
    }

    public void selectPosition(int position)
    {
         for(int i=0;i<mainNavigation.getChildCount();i++)

           {
               ImageView iv = (ImageView) mainNavigation.getChildAt(i);
             if(position==i)
             {
                 iv.setSelected(true);
             }else
             {
                 iv.setSelected(false);
             }
           }
    }

    public void initFragment()
    {
        OneFragment oneFragment = new OneFragment();
        TwoFragment twoFragment = new TwoFragment();
        ThirdFragment thirdFragment = new ThirdFragment();
        FourthFragment fourthFragment = new FourthFragment();
        FiveFragment fiveFragment = new FiveFragment();
        mFragmentList.add(oneFragment);
        mFragmentList.add(twoFragment);
        mFragmentList.add(thirdFragment);
        mFragmentList.add(fourthFragment);
        mFragmentList.add(fiveFragment);
        mManager.beginTransaction().add(R.id.main_container,oneFragment).commit();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if(System.currentTimeMillis()-mNowtime>2000)
            {
                Toast.makeText(this,"快点按退出程序",Toast.LENGTH_LONG).show();
                mNowtime=System.currentTimeMillis();
                return true;
            }else
            {
                finish();
            }
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Glide.get(this).clearMemory();
    }
}


